import java.util.*;

public class Solution1606 {
    public List<Integer> busiestServers(int k, int[] arrival, int[] load) {
        PriorityQueue<Integer> q1=new PriorityQueue<>();
        PriorityQueue<Integer> q2=new PriorityQueue<>();
        PriorityQueue<int[]> q3=new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
        int[] ans=new int[k];
        for (int i = 0; i < k; i++) {
            q1.offer(i);
        }
        for (int i=0;i<arrival.length;i++){
            while (!q3.isEmpty() && q3.peek()[1]<=arrival[i]){
                q1.offer(q3.poll()[0]);
            }
            int t=i%k;
            while (!q1.isEmpty() && q1.peek()<t){
                q2.offer(q1.poll());
            }
            while (!q2.isEmpty() && q2.peek()>=t){
                q1.offer(q2.poll());
            }
            if (!q1.isEmpty()){
                int p=q1.poll();
                q3.offer(new int[]{p,arrival[i]+load[i]});
                ans[p]++;
            }else if (!q2.isEmpty()){
                int p=q2.poll();
                q3.offer(new int[]{p,arrival[i]+load[i]});
                ans[p]++;
            }
        }
        int max= Arrays.stream(ans).max().getAsInt();
        List<Integer> a=new ArrayList<>();
        for (int i=0;i<k;i++){
            if (ans[i]==max){
                a.add(i);
            }
        }
        return a;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1606().busiestServers(2,new int[]{2,3,4,8},new int[]{3,2,4,3}));
    }
}
